import config from "./config";
import { readFileSync } from "fs";
import { IDownloadType } from "../kalendjs-type";

var xl = require('excel4node')
var wb = new xl.Workbook()


var ws = wb.addWorksheet('MaterialFlowSheet');

var style = wb.createStyle({
    font: {
        color: '#FF0800',
        size: 12
    }
});

export const excelnode = function (heads: any, json: any) {
    try {
        var largeText = wb.createStyle({
            font: {
                bold: true
            },
            alignment: {
                vertical: 'center'
            }
        });
        for (let i = 0; i < heads.length; i++) {
            ws.cell(1, i + 1).string(heads[i]).style(largeText);;
        }
        for (let index = 0; index < json.length; index++) {
            let item= json[index]
            for (let i = 0; i < item.length; i++) {
                ws.cell(index + 2, i + 1).string(item[i]);
            }
        }
        wb.write(config.filePath.export + '\\task' + '\\exportlist.xlsx');
        return {
            body: readFileSync(config.filePath.export + '\\task' + '\\exportlist.xlsx'),
            filename: `exportlist.xlsx`,
            path: config.filePath.export + '\\task',
            contentType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
        } as IDownloadType
    } catch (e) {
        // errorHandler(res, e)
    }
}